package com.situ.mall.controller;

import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.User;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("/getProductListPage")
    public String getProductListPage(Integer id, Model model) {
        System.out.println("ProductController.getProductListPage");
        List<Product> list =  productService.selectByCategoryId(id);
        model.addAttribute("list", list);
        return "product_list";
    }


    @RequestMapping("/getProductDetailPage")
    public String getProductDetailPage(Integer id, Model model) {
        System.out.println("ProductController.getProductDetailPage");
        Product product = productService.selectById(id);
        model.addAttribute("product", product);
        return "product_detail";
    }

    @RequestMapping("/getSecondKillPage")
    public String getSecondKillPage() {
        return "second_kill";
    }

    @RequestMapping("/secondKill")
    @ResponseBody
    public Result secondKill(Integer productId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return productService.secondKill(productId, 11);
    }
}
